package com.light.generator.service.impl;

import com.light.generator.dao.GeneratorDao;
import com.light.generator.domain.GeneratorInfo;
import com.light.generator.service.GeneratorService;
import com.light.generator.util.GenUtils;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipOutputStream;

/**
 * @Tittle GeneratorServiceImpl
 * @Description
 * @Author yhr
 * @Date 2019/4/3 12:55
 * @Version 1.0
 */
@Service
public class GeneratorServiceImpl implements GeneratorService {

    @Autowired
    private GeneratorDao generatorDao;

    @Override
    public byte[] generatorCode(GeneratorInfo generatorInfo) throws Exception  {
        File file = new File(generatorInfo.getZipPath());
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ZipOutputStream zip = new ZipOutputStream(new FileOutputStream(file));
        for (String tableName : generatorInfo.getTableNames()) {
            // 查询表信息
            Map<String, String> table = generatorDao.getTableInfo(tableName);
            // 查询列信息
            List<Map<String, String>> columns = generatorDao.getTableColums(tableName);
            //处理
            //dealColumsComments(columns);
            // 生成代码
            GenUtils.generatorCode(generatorInfo,table, columns, zip);
        }
        IOUtils.closeQuietly(zip);
        return outputStream.toByteArray();
    }

}
